/*
 * VHAbstractService.java
 * Created on 7 févr. 2009
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.server.servlet.base;

import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import com.hockeo.vhbase.data.dao.IGDao;
import com.hockeo.vhbase.utils.data.GDataValidator;
import com.hockeo.vhweb.server.convert.VHDaoConverter;

/**
 * Base class for all RPC service implementations.
 *
 * @version $Id$
 * @author jjanke
 */
public abstract class VHAbstractService
{
  private static ThreadLocal<HttpServletRequest> d_servletRequests = new ThreadLocal<HttpServletRequest>();
  private IGDao                                  d_dao;
  private GDataValidator                         d_dataValidator;
  private VHLocaleResolver                       d_localeResolver;
  private VHDaoConverter                         d_daoConverter;

  public final IGDao getDao()
  {
    return d_dao;
  }

  public final void setDao( IGDao dao )
  {
    d_dao = dao;
  }

  public final GDataValidator getDataValidator()
  {
    return d_dataValidator;
  }

  public final void setDataValidator( GDataValidator dataValidator )
  {
    d_dataValidator = dataValidator;
  }

  public VHLocaleResolver getLocaleResolver()
  {
    return d_localeResolver;
  }

  public void setLocaleResolver( VHLocaleResolver localeResolver )
  {
    d_localeResolver = localeResolver;
  }

  public VHDaoConverter getDaoConverter()
  {
    return d_daoConverter;
  }

  public void setDaoConverter( VHDaoConverter daoConverter )
  {
    d_daoConverter = daoConverter;
  }

  /**
   * Sets the servlet request which was used to submit a service request. The request
   * object should be set before any GWT RPC request is actually submitted to a service
   * method.
   *
   * @param rq the request to be set
   */
  public void setThreadLocalRequest( HttpServletRequest rq )
  {
    d_servletRequests.set( rq );
  }

  /**
   * Remove the thread local servlet request.
   */
  public void removeThreadLocalRequest()
  {
    d_servletRequests.remove();
  }

  /**
   * Returns the thread local servlet request. The request must have been set using
   * {@link #setThreadLocalServletRequest(HttpServletRequest)} prior to calling this
   * method.
   */
  protected HttpServletRequest getThreadLocalRequest()
  {
    return d_servletRequests.get();
  }

  /**
   * Convenience method that uses the set locale resolver to get the locale from the
   * request.
   *
   * @param strLocale the locale string (e.g. 'de', 'de_CH' or 'fr_FR_UNIX' etc.)
   * @return the resolved locale
   */
  protected Locale resolveLocale( final String strLocale )
  {
    return d_localeResolver.resolveLocale( strLocale );
  }

  /**
   * Returns the resource bundle providing access to the base resource bundle.
   *
   * @param strLocale the locale for which the resource bundle is requested
   */
  protected ResourceBundle getBaseResources( final String strLocale )
  {
    return getResourceBundle( "com.hockeo.vhweb.server.res.VHBaseRes", strLocale );
  }

  /**
   * Returns a resource bundle matching the locale of the last servlet request.
   *
   * @param strBaseName the name of the resource bundle
   * @param strLocale the locale string (e.g. 'de', 'de_CH' or 'fr_FR_UNIX' etc.)
   */
  private ResourceBundle getResourceBundle( String strBaseName, String strLocale )
  {
    return ResourceBundle.getBundle( strBaseName, resolveLocale( strLocale ) );
  }
}
